/**
 * SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
 * SPDX-License-Identifier: Mulan PSL v2
 */
// eslint-disable-next-line
// @ts-nocheck
import VueRouter from 'vue-router';
import routes from './home/mod_launch/router_config';
import Vue from 'vue';
import App from './app.vue';
import store from 'src/vuex/store';
import * as IntervalTask from './home/mod_launch/interval_task/index';
import { on as bindEvent } from 'wind-dom';
import { debounce } from 'src/util/delay';
import { isFunction } from 'src/util/typeof';
import * as Ajax from 'src/util/ajax';
import beforeInit from './home/mod_launch/before_init';


let router: VueRouter;
let app: Vue & {activeTab: string; isShowWarn: boolean};

let activeTab = '/algorithm_manager';

/**
 * 初始化页面路由
 * @return {VueRouter} 返回路由
 */
function initRouter () {
    router = new VueRouter({
        routes
    });

    // 当前无导航, 注释掉这里导航选中tab高亮的处理.后面有导航了再加回来.
    router.beforeEach((to, form, next) => {

        // 1、路由匹配不上
        // 2、子路由只提取根路径，高亮导航条
        let match = to.path.match(/^(\/[^\/]+).*/);
        if (!to.matched || !to.matched.length || !match) {
            next(`${activeTab}`);
            return;
        }

        activeTab = match[1];
        if (app) {
            app.activeTab = activeTab;
        }
        next();
    });
    return router;
}

/**
 * 加载主页面
 */
/* eslint-disable */

function initApp() {
    let indexScreenList = [/^#\/index/i];
    let checkIsIndexScreen = () => !!indexScreenList.find((reg) => reg.test(window.location.hash));

    app = new Vue({
        store,
        router,
        data: {
            activeTab: activeTab
        },
        computed: {
            isShowWarn () {
                return this.$store.state.authorization.isShowAuthWarn;
            }
        },
        render (h) {
            return h(App, {
                props: {
                    activeTab: this.activeTab,
                    isShowWarn: this.isShowWarn
                },
                ref: 'app'
            })
        }
    });

    app.$mount('#app');

    window.SHARED.$root = app;

    app.$store.commit('updateHeight', app.isShowWarn);
    app.$store.commit('updateWidth');
    app.$store.commit('updateLeftNavWidth');

    // 一些公共的定时任务处理
    IntervalTask.run();

}

/**
 * 绑定 resize 事件
 * */
function initResizeEvent() {
    const RESIZE_DELAY = 16.7;
    let app = window.SHARED.$root;

    bindEvent(window, 'resize', debounce(() => {
        app.$store.commit('updateHeight', app.isShowWarn);
        app.$store.commit('updateWidth');
        app.$store.commit('updateLeftNavWidth');
        let viewComp: vue = (app.$refs.app as vue).$refs.viewComp as vue;

        if (isFunction((viewComp.$options as SafeAny).onResize)) {
            (viewComp.$options as SafeAny).onResize.call(viewComp);
        }
    }, RESIZE_DELAY));
}


function initGlobalConst() {
    window.SHARED.CONST = {
        PRE_SALE_PHONE: '400-806-6868',
        AFTER_SALE_PHONE: '400-630-6430',
        SUBSCRIBE_SERVICE_PHONE: '0731-88043168'
    };
}

/**
 * BBC所有的遮罩层统一都用 defer:0 的效果
 * */
function fixBBCMask() {
    let _maskFn = Vue.prototype.$mask;
    Vue.prototype.$mask = function (el: HTMLElement, options = {}) {
        let me = this;

        return _maskFn.call(me, el, Object.assign({
            defer: 0
        }, options));
    };
}

/**
 * 页面入口
 */
function _init() {

    initGlobalConst();

    Ajax.init();

    // 初始化路由配置
    initRouter();

    // 加载页面主体
    initApp();

    initResizeEvent();

    // 重写 mask
    fixBBCMask();

    Vue.prototype.CSRF = window.SHARED.CSRF;

    document.title = 'AI';

}

export default function init () {
    beforeInit(_init, store);
}